#coding=utf-8
from bson.json_util import dumps
import datetime
from iLibM.loger import *
from models.d_todos import bll_todos

__author__ = 'jy@cjlu.edu.cn'

'''
功能名称:银行
功能介绍:

主体数据表:d_bankstate
'''


from basehandler import basehandler
from config import *
import json


class Add(basehandler):
    """
           增加一条todo
    """
    def get(self):
        self.set_header("Content-Type", "text/html; charset=UTF-8")
        self.set_header("Access-Control-Allow-Origin","*")
        i=self.input()
        t=global_lookup.get_template("f10/T/f0011_add.html")
        self.write(t.render(i=i))
    def post(self):
        self.set_header("Content-Type", "application/json; charset=UTF-8")
        self.set_header("Access-Control-Allow-Origin","*")
        i=self.input()
        try:
            obj=bll_todos()
            i.createtime=datetime.datetime.now()
            i.status="unclear"
            i.createuser="admin"
            i=obj.Save(i)
            self.write(dumps(i))
        except Exception,ex:
            msg="在调用todo的Add时发生了错误，具体信息,"+ex.message
            LogError(msg)
            self.write(dumps({'result':False,'msg':msg}))

class GetTodo(basehandler):
    """
         获取todo数据，参数
    """
    def get(self):
        self.set_header("Content-Type", "text/html; charset=UTF-8")
        self.set_header("Access-Control-Allow-Origin","*")
        i=self.input()
        t=global_lookup.get_template("f10/T/f0011_gettodo.html")
        self.write(t.render(i=i))
    def post(self):
        self.set_header("Content-Type", "application/json; charset=UTF-8")
        self.set_header("Access-Control-Allow-Origin","*")
        i=self.input()
        try:
            obj=bll_todos()
            result=None
            if i.cmd=="all":
                result,recordercount,pagecount=obj.GetPagedRowsByFilterDict(i.pageindex,i.pagesize,{})
            if i.cmd=="byid":
                result=obj.getTodo(i.id)
            if i.cmd=="userunclear":
                result,recordercount,pagecount=obj.getUserUnclearTodo(i.pageindex,i.pagesize,i.username)
            if i.cmd=="userclear":
                result,recordercount,pagecount=obj.getUserClearedTodo(i.pageindex,i.pagesize,i.username)
            r=storage()
            r.result=result.sort([("createtime", -1)])
            r.recordercount=recordercount
            r.pagecount=pagecount
            self.write(dumps(r))

        except Exception,ex:
            msg="在调用todo的Add时发生了错误，具体信息,"+ex.message
            LogError(msg)
            self.write(dumps({'result':False,'msg':msg}))


class UpdateTodo(basehandler):
    def get(self):
        self.set_header("Content-Type", "text/html; charset=UTF-8")
        self.set_header("Access-Control-Allow-Origin","*")
        i=self.input()
        t=global_lookup.get_template("f10/T/f0011_updatetodo.html")
        self.write(t.render(i=i))
    def post(self):
        pass


class ClearTodo(basehandler):
    def get(self):
        self.set_header("Content-Type", "text/html; charset=UTF-8")
        self.set_header("Access-Control-Allow-Origin","*")
        i=self.input()
        t=global_lookup.get_template("f10/T/f0011_cleartodo.html")
        self.write(t.render(i=i))
    def post(self):
        pass



class  DelTodo(basehandler):
    def get(self):
        self.set_header("Content-Type", "text/html; charset=UTF-8")
        self.set_header("Access-Control-Allow-Origin","*")
        i=self.input()
        t=global_lookup.get_template("f10/T/f0011_deltodo.html")
        self.write(t.render(i=i))
    def post(self):
        pass
